package com.util;

import com.alibaba.druid.pool.DruidDataSource;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Author: 柏竹
 * @Description: 服务器工具
 * @Date_Created_in: 2021-03-22 9:10
 * @Modified_By: Sans
 * @Project： 快递e栈
 */
public class DruidUtil {
    
    //采用 Druid 连接池
    private static DruidDataSource dds = null;
    protected Connection connection = null;
    protected PreparedStatement pps = null;
    protected ResultSet rs = null;
    protected int count = 0;
    
    
    static {
        dds = new DruidDataSource();
        ResourceBundle bundle = ResourceBundle.getBundle("linkInfo");
        dds.setDriverClassName(bundle.getString("driver"));
        dds.setUrl(bundle.getString("url"));
        dds.setUsername(bundle.getString("user"));
        dds.setPassword(bundle.getString("password"));
        dds.setInitialSize(Integer.parseInt(bundle.getString("initialSize")));
        dds.setMaxActive(Integer.parseInt(bundle.getString("maxActive")));
        dds.setMaxWait(Integer.parseInt(bundle.getString("maxWait")));
        dds.setValidationQuery(bundle.getString("validationQuery"));
        dds.setRemoveAbandoned(Boolean.parseBoolean(bundle.getString("removeAbandoned")));
        dds.setRemoveAbandonedTimeout(Integer.parseInt(bundle.getString("removeAbandonedTimeout")));
        dds.setLogAbandoned(Boolean.parseBoolean(bundle.getString("logAbandoned")));
    }
    
    //获取连接
    protected Connection getConnection() throws SQLException {
        if (dds != null) {
            connection = dds.getConnection();
        }
        return connection;
    }
    
    /**
     * 获取 预通道
     *
     * @param sql
     * @return 获取 预通道
     */
    protected PreparedStatement getPps(String sql) {
        try {
            connection = getConnection();
            pps = connection.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return pps;
    }
    
    /**
     * 预通道 占位符赋值
     *
     * @param list
     */
    protected void setParams(List list) {
        try {
            if (list != null && list.size() > 0) {
                for (int i = 1 ; i <= list.size() ; i++) {
                    pps.setObject(i , list.get(i - 1));
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    
    /**
     * 查 数据
     *
     * @param sql
     * @param list
     * @return 结果集
     */
    protected ResultSet query(String sql , List list) {
        try {
            getPps(sql);
            if (list != null && list.size() > 0) {
                setParams(list);
            }
            rs = pps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rs;
    }
    
    /**
     * 改 数据
     *
     * @param sql
     * @param list
     * @return 影响数
     */
    protected int update(String sql , List list)
            throws SQLException {
        getPps(sql);
        setParams(list);
        count = pps.executeUpdate();
        return count;
    }
    
    /**
     * 反射处理集
     *
     * @return
     */
    protected List refexClass(Class cla)
            throws SQLException{
        if (rs != null) {
            Method[] declaredMethods = cla.getDeclaredMethods();
            String[] columnNames = null;
            List list = new ArrayList();
    
            ResultSetMetaData metaData = rs.getMetaData();
            count = metaData.getColumnCount();
            columnNames = new String[count];
            for (int i = 0 ; i < count ; i++) {
                columnNames[i] = metaData.getColumnName(i + 1);
            }
    
            try {
                while (rs.next()) {
                    Object o = cla.getDeclaredConstructor().newInstance();
                    //
                    // System.out.println("===========================");
                    for (String columnName : columnNames) {
                        String newColumnName = "set" + columnName;
                        for (Method declaredMethod : declaredMethods) {
                            if (declaredMethod.getName().equalsIgnoreCase(newColumnName)) {
                                //
                                // System.out.println(newColumnName+"\t"+declaredMethod.getName()+"\t"+rs.getObject(columnName));
                                declaredMethod.invoke(o , rs.getObject(columnName));
                                break;
                            }
                        }
                    }
                    list.add(o);
                }
                if(list.size() <= 0){
                    return null;
                }
                return list;
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    /**
     * 关闭资源
     */
    protected void closeDaoAll(){
        try {
            if (rs != null) {
                rs.close();
            }
            if (pps != null) {
                pps.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    
    /**
     * 关闭服务器
     */
    protected void closeServer(){
        if (connection != null) {
            dds.close();
        }
    }
    
}